#include "sdl2/SDL.h"
#include "SDL2/SDL_syswm.h"
#include "sdl2/SDL_opengl.h"
#include <SDL2/SDL_egl.h>

#include "EGL/egl.h"
#include <stdio.h>
int Init_Angle_EGL(SDL_Window* win);
void GL_Init(SDL_Window* win)
{
    Init_Angle_EGL(win);

//	int realw = -1;
//	int realh = -1;
//	SDL_GetWindowSize(win, &realw, &realh);
//
//	SDL_SysWMinfo info;
//	SDL_VERSION(&info.version);//must do it before SDL_GetWindowWMInfo
//	void* display;
//	void* nwh;
//	if (SDL_GetWindowWMInfo(win, &info))
//	{
//		if (info.subsystem == SDL_SYSWM_TYPE::SDL_SYSWM_WINDOWS)
//		{
//#if defined(SDL_VIDEO_DRIVER_WINDOWS)
//			nwh = (void*)info.info.win.window;
//			display = (void*)info.info.win.hdc;
//#endif
//		}
//		else if (info.subsystem == SDL_SYSWM_TYPE::SDL_SYSWM_X11)
//		{
//#if defined(SDL_VIDEO_DRIVER_X11)
//			pdata.ndt = info.info.x11.display;
//			pdata.nwh = (void*)info.info.x11.window;
//#endif
//		}
//		else if (info.subsystem == SDL_SYSWM_TYPE::SDL_SYSWM_WAYLAND)
//		{
//#if defined(SDL_VIDEO_DRIVER_WAYLAND)
//			pdata.ndt = info.info.wl.display;
//			pdata.nwh = (void*)info.info.wl.surface;
//#endif
//		}
//		else
//		{
//			throw "only support x11 fornow.";
//		}
//	}
//
//	HDC dc = GetDC((HWND)win);
//	EGLDisplay ndist = eglGetDisplay((EGLNativeDisplayType)dc);
//	
//	EGLint major = 0;
//	EGLint minor = 0;
//	//EGLNativeDisplayType ndist = (EGLNativeDisplayType)nwh;
//	bool b = eglInitialize(ndist, &major, &minor);
//	
//	printf("ver %d,%d", major, minor);
//	EGLConfig configs[32];
//	int numConfigs;
//	eglGetConfigs(ndist, configs, 0, &numConfigs);
//	eglChooseConfig(ndist,)
//	eglCreateWindowSurface(ndist,)
}



EGLDisplay g_display;
EGLSurface g_surface;
void GL_Loop()
{
	glViewport(0, 0, 1280, 800);
	glClearColor(1.0f, 0.3f, 0.5f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);

    eglSwapBuffers(g_display, g_surface);
	//glFlush();
	//eglSwapBuffers
		//SDL_GL_SwapWindow(winMain);
}



int Init_Angle_EGL(SDL_Window* win)
{
    EGLint configAttribList[] =
    {
        EGL_RED_SIZE, 8,
        EGL_GREEN_SIZE, 8,
        EGL_BLUE_SIZE, 8,
        EGL_ALPHA_SIZE, 8,
        EGL_DEPTH_SIZE, 8,
        EGL_STENCIL_SIZE, 8,
        EGL_SAMPLE_BUFFERS, 1,
        EGL_NONE };
    EGLint surfaceAttribList[] =
    {
        EGL_POST_SUB_BUFFER_SUPPORTED_NV, EGL_FALSE,
        EGL_NONE, EGL_NONE };

    EGLint numConfigs;
    EGLint majorVersion;
    EGLint minorVersion;
    EGLDisplay display;
    EGLContext context;
    EGLSurface surface;
    EGLConfig config;
    EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };

    SDL_SysWMinfo info;
    SDL_VERSION(&info.version); // initialize info structure with SDL version info
    SDL_bool get_win_info = SDL_GetWindowWMInfo(win, &info);
    SDL_assert_release(get_win_info);
    EGLNativeWindowType hWnd = info.info.win.window;

    // Get Display
    void* hdc = GetDC(hWnd);
    display = eglGetDisplay((EGLNativeDisplayType)hdc); // EGL_DEFAULT_DISPLAY
    if (display == EGL_NO_DISPLAY)
    {
        return EGL_FALSE;
    }

    // Initialize EGL
    if (!eglInitialize(display, &majorVersion, &minorVersion))
    {
        return EGL_FALSE;
    }

    // Get configs
    if (!eglGetConfigs(display, NULL, 0, &numConfigs))
    {
        return EGL_FALSE;
    }

    // Choose config
    if (!eglChooseConfig(display, configAttribList, &config, 1, &numConfigs))
    {
        return EGL_FALSE;
    }

    // Create a surface
    surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)hWnd, surfaceAttribList);
    if (surface == EGL_NO_SURFACE)
    {
        return EGL_FALSE;
    }

    // Create a GL context
    context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
    if (context == EGL_NO_CONTEXT)
    {
        return EGL_FALSE;
    }

    // Make the context current
    if (!eglMakeCurrent(display, surface, surface, context))
    {
        return EGL_FALSE;
    }
    const char* rd = (const char* )glGetString(GL_RENDERER);
    const char* verstr = (const char* )glGetString(GL_VERSION);
    const char* shaderstr = (const char* )glGetString(GL_SHADING_LANGUAGE_VERSION);
    printf("GL_RENDERER: %s\n",rd );
    printf("GL_VERSION: %s\n", verstr);
    printf("GL_SHADING_LANGUAGE_VERSION: %s\n", shaderstr);
    //swap->display = display;
    //swap->surface = surface;

    g_display = display;
    g_surface = surface;
    return EGL_TRUE;
}

